dd0=$(pwd)

######################################################################

#full run
dsets=$(find $(pwd)/*/LHC*/ -type d | grep -v "/cohe" )
jobid=full

#short test
#dsets=$(echo $(pwd)/DATA_all/LHC10hCEN0_DATA_RAA_ETA0/ )
#jobid=short

ecor=_HASENERGYLOSSCORRECTION
tofopt=$(echo TOF1SIGMA TOF2SIGMA noTOF)

######################################################################
#FF
#dsets=$(echo $(pwd)/DATA*/histRec*)
#jobid=FF
#ecor=_HASENERGYLOSSCORRECTION_NoHighDensity
#tofopt=$(echo noTOF)

#_WithRef #_NoHighDensity_MeanFixTrue_ResolutionFixTrue
######################################################################

mm=$(echo  mBB aleph) 
#aleph mBB  SatuAl SatuBb 
anc=$(echo anchor4)
regopt=$(echo 0) # 1 2 3 4
sigma=$(echo 2000 3000)
intp=$(echo 2)

for ii in $dsets; do
    for method in $(echo $mm); do 
        for jj in $(echo $anc); do
            for reg in $(echo $regopt); do 
                for tof in $(echo $tofopt); do
                    dir=$ii/coherent/$tof/$method\_$jj\_RegOpt$reg$ecor
                    echo $dir; 
                    mkdir -p $dir; 
                    
                    cd $dir    
                    
                    cp ~/svn.log .; cp $dd0/../{fith,mn2d,dofith.sh,domn2d.sh} .
                    
                    jobfile=$jobid\subfit.sh

                    cat > $jobfile <<EOF
uname -a

source /hera/alice/xlu/alice/script/hera-evn.sh -n

cd $dir

echo ls
ls
echo

echo pwd
pwd
echo

echo go

./dofith.sh ../../../histRec*.root

for res in \$(echo $sigma); do 
    for dbin in \$(echo $intp); do 
        echo hi
        ./domn2d.sh \$res \$dbin
    done; 
done

EOF

                    qsub -cwd -l h_rt=10:0:0,h_rss=2G  ./$jobfile 
# chmod +x $jobfile;./$jobfile               

                    cd $dd0
                done;
            done;
        done;
    done;
done


exit

#################################################################
#demonstrate bin0 correction

for ii in $(find MC*/LHC* -type d)
do
    #echo $ii
    cd $ii
    ff=$(find histGen*.root)
    echo $ff
    cp ~/svn.log svnbin0.log
    /hera/alice/xlu/workspace/hera2dbetabloch/drawMCbin0 $ff > seebin0.log
    cd -
done


#################################################################
#generate Efficiency
for ii in $(find MC*/LHC* -type d)
do
    echo $ii
    cd $ii
    rm -f OutEff*.root effsvn.log seeEffPt.log
    ls
    echo
    cp ~/svn.log effsvn.log
    /hera/alice/xlu/workspace/hera2dbetabloch/calcEffMC histGen* histRec* PsVrecVzhmcpdgVsPt > seeEffPt.log
    #~/.task/2dbetabloch/calcEffMC histGen* histRec* mcpdgVsEta > seeEffEta.log
    ls
    echo
    echo
    cd -
done

#################################################################
# bad_moveaway used also for TOF cases
#for ii in $(grep ierr *co*/*/*/ID*/see.log| grep "nbeta 100"|grep "ierr 4"| grep final | awk -F\/ '{print $1, $2, $3, $4}' | tr -s ' ' '/'); do find $ii -type d; jj=$(echo $ii | tr -s '/' '-'); echo $jj; mv $ii bad_moveaway/$jj; done

#idff
for ii in $(grep ierr *co*/*/ID*/see.log| grep "nbeta 100"|grep "ierr 4"| grep final | awk -F\/ '{print $1, $2, $3}' | tr -s ' ' '/'); do find $ii -type d; jj=$(echo $ii | tr -s '/' '-'); echo $jj; mv $ii bad_moveaway/$jj; done

#################################################################

#generate fraction from PT
#for ii in $(find MC*/LHC* -type d ); 
for ii in $(find hist* -type d ); 
do 
    echo $ii; cd $ii; cp ~/svn.log .; /hera/alice/xlu/workspace/hera2dbetabloch/getMCtrueFromPT histRec*.root 20 > seegetMCtrue20.log ;cd -; done

#################################################################
#################################################################
###########################################################################

#draw secondary fraction vs. pT
#for ii in  $(find MC_*/LHC10d*MC*ETA3* -type d); 
for ii in  $(find MC_*/LHC* -type d); 
do 
    dir=OutputSecondary/$ii

    echo $dir
    mkdir $dir; 
    cd $dir

pwd > gsi_dir

    file=$(find ../../../$ii/histRec*.root)
    echo $file
    cp ~/.task/2dbetabloch/plotcode/drawSecoFrac.C .
    root -b -l -q 'drawSecoFrac.C("'$file'")'
    cd -
done

#################################################################

dir=$(pwd); for ii in $(find cmpEtaGrID* -type d| grep -v /); do echo $ii; cd $ii; ../../alldo.sh; cd cmpPeriod; ./do*.sh > see.log; cd $dir; done

###########################################################################
res=3000
tag=aleph_anchor4_RegOpt0_HASENERGYLOSSCORRECTION
#SatuBb_anchor4_RegOpt0_HASENERGYLOSSCORRECTION_WithRef
#mBB_anchor4_RegOpt0_HASENERGYLOSSCORRECTION_WithRef
#

/u/xlu/.task/2dbetabloch/sub-doPlot-forthesis.sh $tag $res


#################################################################

ecor=_HASENERGYLOSSCORRECTION #_WithRef #_NoHighDensity_MeanFixTrue_ResolutionFixTrue
anc=$(echo anchor4)
mm=$(echo  mBB aleph) #aleph mBB  SatuAl SatuBb 
regopt=$(echo 0) # 1 2 3 4
sigma=$(echo 2000 3000)
intp=$(echo 2)

#dsets=$(find LHC11a_without_SDD_DATA_RAA_ETA3_POS -type d)
#dsets=$(find 10-d-e-DATA_RAA_ETA0 -type d | grep -v 0/)
#dsets=$(find 11a*ETA3* -type d | grep -v "/")
#dsets=$(find 10h*ETA3*CEN0* -type d | grep -v "/")
#dsets=$(find 10hCEN{0,5}*ETA{0,1,2,3}* -type l)
#dsets=$(find 10hCEN{1,4}*ETA{0,1,2,3}* -type l)
#dsets=$(find 10hCEN{2,3}*ETA{0,1,2,3}* -type l)

#dsets=$(find *11a*ETA{0,1,2,3}* -type d | grep -v "/")
#dsets=$(find *10e*ETA{0,1,2,3}* -type d | grep -v "/")
#dsets=$(find *10d*ETA{0,1,2,3}* -type d | grep -v "/")
#dsets=$(find *10-d-e*ETA{0,1,2,3}* -type d | grep -v "/")
#dsets=$(find *10hCEN{0,5}*ETA{0,1,2,3}* -type d)
#dsets=$(find *10hCEN{1,4}*ETA{0,1,2,3}* -type d)
dsets=$(find *10hCEN{2,3}*ETA{0,1,2,3}* -type d)

#dsets=$(find histRecInclusiveTP*ETA* | grep -v all/)
#dsets=$(find histRecIn*_ETA{0,1,2,3}*_JPT{0,all} | grep -v /)
#dsets=$(find histRecIDFFHist_LHC*_JPT{0,all} | grep -v / )
for method in $(echo $mm); do for reg in $(echo $regopt); do for ii in $dsets; do for jj in $(echo $anc); do
                dir=$ii/coherent/$method\_$jj\_RegOpt$reg$ecor
                echo $dir; mkdir $dir; cd $dir    
                
                cp ~/svn.log .; cp /u/xlu/.task/2dbetabloch/{fith,mn2d} .
                /u/xlu/.task/2dbetabloch/dofith.sh ../../histRec*.root
                
                for res in $(echo $sigma); do for dbin in $(echo $intp); do 
                        /u/xlu/.task/2dbetabloch/domn2d.sh $res $dbin
                    done; done
                cd -
            done;done;done;done

exit
